<?php

/*******************************************************************************
 Version: 1.11 ($Rev: 175 $)
 Website: http://code.google.com/p/kindleweb/
 Author: Martin Maly <maly@maly.cz>
 Contributions by:
 Radek Hulan (code cleaning, ideas)
 Licensed under The MIT License
 Redistributions of files must retain the above copyright notice.
 *******************************************************************************/

set_time_limit(0);

define('PUBNAME', 'E15');
define('CACHENAME', 'e15');

define('ROOTURL', 'http://www.e15.cz');

require('./config.php');
require(ROOTDIR.'simple_html_dom.php');

function tc($href) {
	$h = md5($href);
	if (file_exists(CACHEDIR.$h)) return file_get_contents(CACHEDIR.$h);
	$f = file_get_contents($href);
	file_put_contents(CACHEDIR.$h,$f);
	return $f;
}

function grabjpg($href) {
	$h = md5($href).'.jpg';
	$f = file_get_contents($href);
	file_put_contents(CACHEDIR.$h,$f);
	return $h;
}

//site-depending code
//this function should scrap the title page
//and return an array with links to the articles

function scraping_title() {
	// create HTML DOM
	$html = file_get_html(ROOTURL . '/');
	// get article block
	foreach($html->find('div[class=article]') as $article) {
		// get link
		$item['href'] = trim($article->find('a', 0)->href);
		$ret[] = $item;
	}
	// clean up memory
	$html->clear();
	unset($html);
	return $ret;
}

//this function should scrap the articles
//and return an array with title, perex, body and image

function scraping_article($href) {
	// create HTML DOM
	$html = str_get_html(tc(ROOTURL.$href));
	$a = $html->find('div[id=article]');
	$article=$a[0];
	// get title
	$item['title'] = trim($article->find('h1', 0)->plaintext);
	// get perex
	$item['perex'] = trim($article->find('p[class=perex]', 0)->plaintext);
	// get body
	$body = $article->find('div[class=body]', 0)->children();
	$out = '';
	foreach($body as $n) {
		if ($n->tag == 'p')
		$out.='<p>'.$n->plaintext.'</p>';
		if ($n->tag == 'img')
		$item['img']='<p><img src="'.grabjpg($n->src).'"></p>';
	}
	$item['body'] = $out;
	// clean up memory
	$html->clear();
	unset($html);
	return $item;
}


//grabbing content

$arr = array();

if (file_exists(CACHEDIR.CACHENAME.'a')) $arr = unserialize(file_get_contents(CACHEDIR.CACHENAME.'a'));

$ret = scraping_title();

foreach($ret as $v) {
	$h = md5($v['href']);
	if (!isset($arr[$h])) {
		$x = scraping_article($v['href']);
		$out='';
		$out.=('<h1>'.$x['title'].'</h1>');
		$out.=$x['img'];
		$out.=('<p><b>'.$x['perex'].'</b></p>');
		$body = $x['body'];
		$out.=('<p>'.$body.'</p>');
		$arr[$h] = $out;
	}
}

file_put_contents(CACHEDIR.CACHENAME.'a',serialize($arr));

if (isset($_GET['grabonly'])) die('grab OK');



//Convert content to .mobi and sending to Kindle

$arr = unserialize(file_get_contents(CACHEDIR.CACHENAME.'a'));

//create HTML

$out='<html><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><title>'.PUBNAME.' z '.date('d.m.Y').'</title></head><body>';
foreach($arr as $v) $out.=$v;
$out.='</body></html>';


file_put_contents(CACHEDIR.CACHENAME.'out.html',$out);

//OPF preparing
$f = file_get_contents(ROOTDIR.CACHENAME.'.opf');
$f = str_replace('!!date!!', date('Y-m-d'), $f);
file_put_contents(CACHEDIR.CACHENAME.'.opf', $f);

//image
$f = file_get_contents(ROOTDIR.CACHENAME.'.png');
file_put_contents(CACHEDIR.CACHENAME.'.png', $f);

chdir(CACHEDIR);
exec(ROOTDIR.KINDLEGEN.' '.CACHENAME.'.opf', $o);

$to = KINDLEEMAIL;
$subject = PUBNAME. " z ".date("d.m.Y");

$random_hash = md5(date('r', time()));
$headers = "From: ".MYEMAIL."\r\nReply-To: ".MYEMAIL;
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

$attachment = chunk_split(base64_encode(file_get_contents(CACHEDIR.CACHENAME.'.mobi')));

ob_start();
//quick and dirty; no time to experiment 
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

E15 - Ekonomika - byznys - finance

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="<?php echo CACHENAME; ?>.mobi" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--


<?php

$message = ob_get_clean();

$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
echo (DELETECMD.CACHEDIR.'*');
exec(DELETECMD.CACHEDIR.'*');

?>